package fun._6742.controller;


import fun._6742.model.valueobject.ProductVO;
import fun._6742.service.CategoryService;
import fun._6742.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuQ
 * @since 2022-09-21
 */
@Api(tags = "商品")
@RestController
@RequestMapping("api/product/v1")
public class ProductController {

    @Resource
    ProductService productService;
    @Resource
    CategoryService categoryService;

    @ApiOperation(value = "列表", notes = "分页查询商品列表")
    @GetMapping("page")
    public Map<String, Object> pageProductList(
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "100") int size) {
        return productService.findPage(page, size);
    }

    @ApiOperation(value = "分类列表", notes = "分页查询分类列表")
    @GetMapping("category")
    public Object pageCategoryList(
            @ApiParam(value = "id") @RequestParam(value = "id", defaultValue = "-1") long id,
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "10") int size) {
        if (id > 0) {
            return categoryService.findOneCategory(id);
        } else {
            return categoryService.listAllCategory(page, size);
        }
    }

    @ApiOperation(value = "详情")
    @GetMapping("/detail/{product_id}")
    public ProductVO findDetail(@ApiParam(value = "商品id") @PathVariable(value = "product_id", required = true) long productID) {
        return productService.findDetail(productID);
    }

    @ApiOperation(value = "查询所有品牌和数目")
    @GetMapping("/brand")
    public Map<String, Integer> findAllBrand() {
        return productService.findAllBrand();
    }

    @ApiOperation(value = "根据品牌查商品")
    @GetMapping("/pageByBrand")
    public Map<String, Object> findProductByBrand(
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "100") int size,
            @ApiParam(value = "品牌名称") @RequestParam(value = "brand", required = true) String brand) {
        return productService.findPageByBrand(brand, page, size);
    }

}

